package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.VideoPlayRecyclerViewAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.RecommendVideo;
import com.example.myapplication.entity.RelatedVideo;
import com.example.myapplication.entity.VideoDetailInfo;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class    VideoPlayDetailFragment extends Fragment {
    private View view;
    private RecyclerView recycler_view_video_play;
    private ImageView user_img;
    private TextView user_name;
    private TextView title_video_play;
    private TextView like;
    private TextView downLoad;
    private TextView heart;
    private TextView share;
    private RecommendVideo.DatasBean datasBean;
    private VideoPlayRecyclerViewAdapter adapter;
    private List<RelatedVideo.DataBean> datasBeanList;
    private TextView fans;

    public VideoPlayDetailFragment() {

    }

    public static VideoPlayDetailFragment newInstance(Bundle bundle) {
        VideoPlayDetailFragment fragment = new VideoPlayDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_video_play_detail,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        recycler_view_video_play = view.findViewById(R.id.recycler_view_video_play);
        user_img = view.findViewById(R.id.user_img);
        user_name = view.findViewById(R.id.user_name);
        title_video_play = view.findViewById(R.id.title_video_play);
        fans  = view.findViewById(R.id.fans);
        like = view.findViewById(R.id.like);
        downLoad = view.findViewById(R.id.download);
        heart = view.findViewById(R.id.heart);
        share = view.findViewById(R.id.share);
        datasBean = (RecommendVideo.DatasBean) getArguments().getSerializable("video");
        user_name.setText(datasBean.getData().getCreator().getNickname());
        title_video_play.setText(datasBean.getData().getTitle());
        fans.setText(datasBean.getData().getCreator().getUserType()+" 关注");

        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options1= RequestOptions
                .bitmapTransform(roundedCorners)
                .circleCrop();
        Glide.with(this).load(datasBean.getData().getCreator().getAvatarUrl())
                .centerCrop()
                .error(R.drawable.ic_baseline_refresh_24)
                .placeholder(R.drawable.ic_baseline_refresh_24)
                .apply(options1)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(user_img);

        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());
        recycler_view_video_play.setLayoutManager(manager);
        adapter = new VideoPlayRecyclerViewAdapter(getContext());
        recycler_view_video_play.setAdapter(adapter);
        loadData(datasBean.getData().getVid());
        loadDetailInfo(datasBean.getData().getVid());
    }

    private void loadData(String id) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<RelatedVideo> call = service.getRelatedVideo(id);
        call.enqueue(new Callback<RelatedVideo>() {
            @Override
            public void onResponse(Call<RelatedVideo> call, Response<RelatedVideo> response) {
                RelatedVideo baseResponse = response.body();
                datasBeanList = baseResponse.getData();
                adapter.setData(datasBeanList);
            }

            @Override
            public void onFailure(Call<RelatedVideo> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
    private void loadDetailInfo(String id) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<VideoDetailInfo> call = service.getVideoDetailInfo(id);
        call.enqueue(new Callback<VideoDetailInfo>() {
            @Override
            public void onResponse(Call<VideoDetailInfo> call, Response<VideoDetailInfo> response) {
                VideoDetailInfo baseResponse = response.body();
                like.setText(baseResponse.getLikedCount()+" ");
                share.setText(baseResponse.getShareCount()+" ");
                heart.setText(baseResponse.getCommentCount()+" ");
            }

            @Override
            public void onFailure(Call<VideoDetailInfo> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
